home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / patch / patch.zoo / pch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-10  |  28.3 KB  |  1,140 lines

  1. /* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
  2.  *
  3.  * $Log:    pch.c,v $
  4.  * Revision 2.0.1.7  88/06/03  15:13:28  lwall
  5.  * patch10: Can now find patches in shar scripts.
  6.  * patch10: Hunks that swapped and then swapped back could core dump.
  7.  * 
  8.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  9.  * pch_swap didn't swap p_bfake and p_efake.
  10.  * 
  11.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  12.  * Improved responses to mangled patches.
  13.  * 
  14.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  15.  * New-style context diffs caused double call to free().
  16.  * 
  17.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  18.  * Fixed problem where a long pattern wouldn't grow the hunk.
  19.  * Also restored p_input_line when backtracking so error messages are right.
  20.  * 
  21.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  22.  * New-style delete triggers spurious assertion error.
  23.  * 
  24.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  25.  * Could falsely report new-style context diff.
  26.  * 
  27.  * Revision 2.0  86/09/17  15:39:37  lwall
  28.  * Baseline for netwide release.
  29.  * 
  30.  */
  31.  
  32. #include "EXTERN.h"
  33. #include "common.h"
  34. #include "util.h"
  35. #include "INTERN.h"
  36. #include "pch.h"
  37.  
  38. /* Patch (diff listing) abstract type. */
  39.  
  40. static long p_filesize;            /* size of the patch file */
  41. static LINENUM p_first;            /* 1st line number */
  42. static LINENUM p_newfirst;        /* 1st line number of replacement */
  43. static LINENUM p_ptrn_lines;        /* # lines in pattern */
  44. static LINENUM p_repl_lines;        /* # lines in replacement text */
  45. static LINENUM p_end = -1;        /* last line in hunk */
  46. static LINENUM p_max;            /* max allowed value of p_end */
  47. static LINENUM p_context = 3;        /* # of context lines */
  48. static LINENUM p_input_line = 0;    /* current line # from patch file */
  49. static char **p_line = Null(char**);    /* the text of the hunk */
  50. static short *p_len = Null(short*);    /* length of each line */
  51. static char *p_char = Nullch;        /* +, -, and ! */
  52. static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  53. static int p_indent;            /* indent to patch */
  54. static LINENUM p_base;            /* where to intuit this time */
  55. static LINENUM p_bline;            /* line # of p_base */
  56. static LINENUM p_start;            /* where intuit found a patch */
  57. static LINENUM p_sline;            /* and the line number for it */
  58. static LINENUM p_hunk_beg;        /* line number of current hunk */
  59. static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  60. static LINENUM p_bfake = -1;        /* beg of faked up lines */
  61.  
  62. /* Prepare to look for the next patch in the patch file. */
  63.  
  64. void
  65. re_patch()
  66. {
  67.     p_first = Nulline;
  68.     p_newfirst = Nulline;
  69.     p_ptrn_lines = Nulline;
  70.     p_repl_lines = Nulline;
  71.     p_end = (LINENUM)-1;
  72.     p_max = Nulline;
  73.     p_indent = 0;
  74. }
  75.  
  76. /* Open the patch file at the beginning of time. */
  77.  
  78. void
  79. open_patch_file(filename)
  80. char *filename;
  81. {
  82.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  83.     pfp = fopen(TMPPATNAME, "w");
  84.     if (pfp == Nullfp)
  85.         fatal2("patch: can't create %s.\n", TMPPATNAME);
  86.     while (fgets(buf, sizeof buf, stdin) != Nullch)
  87.         fputs(buf, pfp);
  88.     Fclose(pfp);
  89.     filename = TMPPATNAME;
  90.     }
  91.     pfp = fopen(filename, "r");
  92.     if (pfp == Nullfp)
  93.     fatal2("patch file %s not found\n", filename);
  94. #ifdef TOS
  95.     (void)stat(filename, &filestat);
  96. #else
  97.     Fstat(fileno(pfp), &filestat);
  98. #endif
  99.     p_filesize = filestat.st_size;
  100.     next_intuit_at(0L,1L);            /* start at the beginning */
  101.     set_hunkmax();
  102. }
  103.  
  104. /* Make sure our dynamically realloced tables are malloced to begin with. */
  105.  
  106. void
  107. set_hunkmax()
  108. {
  109. #ifndef lint
  110.     if (p_line == Null(char**))
  111.     p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  112.     if (p_len == Null(short*))
  113.     p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  114. #endif
  115.     if (p_char == Nullch)
  116.     p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  117. }
  118.  
  119. /* Enlarge the arrays containing the current hunk of patch. */
  120.  
  121. void
  122. grow_hunkmax()
  123. {
  124.     hunkmax *= 2;
  125.     /* 
  126.      * Note that on most systems, only the p_line array ever gets fresh memory
  127.      * since p_len can move into p_line's old space, and p_char can move into
  128.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  129.      */
  130.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  131. #ifndef lint
  132.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  133.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  134.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  135. #endif
  136.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  137.     return;
  138.     if (!using_plan_a)
  139.     fatal1("patch: out of memory (grow_hunkmax)\n");
  140.     out_of_mem = TRUE;        /* whatever is null will be allocated again */
  141.                 /* from within plan_a(), of all places */
  142. }
  143.  
  144. /* True if the remainder of the patch file contains a diff of some sort. */
  145.  
  146. bool
  147. there_is_another_patch()
  148. {
  149.     if (p_base != 0L && p_base >= p_filesize) {
  150.     if (verbose)
  151.         say1("done\n");
  152.     return FALSE;
  153.     }
  154.     if (verbose)
  155.     say1("Hmm...");
  156.     diff_type = intuit_diff_type();
  157.     if (!diff_type) {
  158.     if (p_base != 0L) {
  159.         if (verbose)
  160.         say1("  Ignoring the trailing garbage.\ndone\n");
  161.     }
  162.     else
  163.         say1("  I can't seem to find a patch in there anywhere.\n");
  164.     return FALSE;
  165.     }
  166.     if (verbose)
  167.     say3("  %sooks like %s to me...\n",
  168.         (p_base == 0L ? "L" : "The next patch l"),
  169.         diff_type == CONTEXT_DIFF ? "a context diff" :
  170.         diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  171.         diff_type == NORMAL_DIFF ? "a normal diff" :
  172.         "an ed script" );
  173.     if (p_indent && verbose)
  174.     say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  175.     skip_to(p_start,p_sline);
  176.     while (filearg[0] == Nullch) {
  177.     if (force) {
  178.         say1("No file to patch.  Skipping...\n");
  179.         filearg[0] = savestr(bestguess);
  180.         return TRUE;
  181.     }
  182.     ask1("File to patch: ");
  183.     if (*buf != '\n') {
  184.         if (bestguess)
  185.         free(bestguess);
  186.         bestguess = savestr(buf);
  187.         filearg[0] = fetchname(buf, 0, FALSE);
  188.     }
  189.     if (filearg[0] == Nullch) {
  190.         ask1("No file found--skip this patch? [n] ");
  191.         if (*buf != 'y') {
  192.         continue;
  193.         }
  194.         if (verbose)
  195.         say1("Skipping patch...\n");
  196.         filearg[0] = fetchname(bestguess, 0, TRUE);
  197.         skip_rest_of_patch = TRUE;
  198.         return TRUE;
  199.     }
  200.     }
  201.     return TRUE;
  202. }
  203.  
  204. /* Determine what kind of diff is in the remaining part of the patch file. */
  205.  
  206. int
  207. intuit_diff_type()
  208. {
  209.     Reg4 long this_line = 0;
  210.     Reg5 long previous_line;
  211.     Reg6 long first_command_line = -1;
  212.     long fcl_line;
  213.     Reg7 bool last_line_was_command = FALSE;
  214.     Reg8 bool this_is_a_command = FALSE;
  215.     Reg9 bool stars_last_line = FALSE;
  216.     Reg10 bool stars_this_line = FALSE;
  217.     Reg3 int indent;
  218.     Reg1 char *s;
  219.     Reg2 char *t;
  220.     char *indtmp = Nullch;
  221.     char *oldtmp = Nullch;
  222.     char *newtmp = Nullch;
  223.     char *indname = Nullch;
  224.     char *oldname = Nullch;
  225.     char *newname = Nullch;
  226.     Reg11 int retval;
  227.     bool no_filearg = (filearg[0] == Nullch);
  228.  
  229.     ok_to_create_file = FALSE;
  230.     Fseek(pfp, p_base, 0);
  231.     p_input_line = p_bline - 1;
  232.     for (;;) {
  233.     previous_line = this_line;
  234.     last_line_was_command = this_is_a_command;
  235.     stars_last_line = stars_this_line;
  236.     this_line = ftell(pfp);
  237.     indent = 0;
  238.     p_input_line++;
  239.     if (fgets(buf, sizeof buf, pfp) == Nullch) {
  240.         if (first_command_line >= 0L) {
  241.                     /* nothing but deletes!? */
  242.         p_start = first_command_line;
  243.         p_sline = fcl_line;
  244.         retval = ED_DIFF;
  245.         goto scan_exit;
  246.         }
  247.         else {
  248.         p_start = this_line;
  249.         p_sline = p_input_line;
  250.         retval = 0;
  251.         goto scan_exit;
  252.         }
  253.     }
  254.     for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  255.         if (*s == '\t')
  256.         indent += 8 - (indent % 8);
  257.         else
  258.         indent++;
  259.     }
  260.     for (t=s; isdigit(*t) || *t == ','; t++) ; 
  261.     this_is_a_command = (isdigit(*s) &&
  262.       (*t == 'd' || *t == 'c' || *t == 'a') );
  263.     if (first_command_line < 0L && this_is_a_command) { 
  264.         first_command_line = this_line;
  265.         fcl_line = p_input_line;
  266.         p_indent = indent;        /* assume this for now */
  267.     }
  268.     if (!stars_last_line && strnEQ(s, "*** ", 4))
  269.         oldtmp = savestr(s+4);
  270.     else if (strnEQ(s, "--- ", 4))
  271.         newtmp = savestr(s+4);
  272.     else if (strnEQ(s, "Index:", 6))
  273.         indtmp = savestr(s+6);
  274.     else if (strnEQ(s, "Prereq:", 7)) {
  275.         for (t=s+7; isspace(*t); t++) ;
  276.         revision = savestr(t);
  277.         for (t=revision; *t && !isspace(*t); t++) ;
  278.         *t = '\0';
  279.         if (!*revision) {
  280.         free(revision);
  281.         revision = Nullch;
  282.         }
  283.     }
  284.     if ((!diff_type || diff_type == ED_DIFF) &&
  285.       first_command_line >= 0L &&
  286.       strEQ(s, ".\n") ) {
  287.         p_indent = indent;
  288.         p_start = first_command_line;
  289.         p_sline = fcl_line;
  290.         retval = ED_DIFF;
  291.         goto scan_exit;
  292.     }
  293.     stars_this_line = strnEQ(s, "********", 8);
  294.     if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
  295.          strnEQ(s, "*** ", 4)) {
  296.         if (!atol(s+4))
  297.         ok_to_create_file = TRUE;
  298.         /* if this is a new context diff the character just before */
  299.         /* the newline is a '*'. */
  300.         while (*s != '\n')
  301.         s++;
  302.         p_indent = indent;
  303.         p_start = previous_line;
  304.         p_sline = p_input_line - 1;
  305.         retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  306.         goto scan_exit;
  307.     }
  308.     if ((!diff_type || diff_type == NORMAL_DIFF) && 
  309.       last_line_was_command &&
  310.       (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  311.         p_start = previous_line;
  312.         p_sline = p_input_line - 1;
  313.         p_indent = indent;
  314.         retval = NORMAL_DIFF;
  315.         goto scan_exit;
  316.     }
  317.     }
  318.   scan_exit:
  319.     if (no_filearg) {
  320.     if (indtmp != Nullch)
  321.         indname = fetchname(indtmp, strippath, ok_to_create_file);
  322.     if (oldtmp != Nullch)
  323.         oldname = fetchname(oldtmp, strippath, ok_to_create_file);
  324.     if (newtmp != Nullch)
  325.         newname = fetchname(newtmp, strippath, ok_to_create_file);
  326.     if (oldname && newname) {
  327.         if (strlen(oldname) < strlen(newname))
  328.         filearg[0] = savestr(oldname);
  329.         else
  330.         filearg[0] = savestr(newname);
  331.     }
  332.     else if (oldname)
  333.         filearg[0] = savestr(oldname);
  334.     else if (newname)
  335.         filearg[0] = savestr(newname);
  336.     else if (indname)
  337.         filearg[0] = savestr(indname);
  338.     }
  339.     if (bestguess) {
  340.     free(bestguess);
  341.     bestguess = Nullch;
  342.     }
  343.     if (filearg[0] != Nullch)
  344.     bestguess = savestr(filearg[0]);
  345.     else if (indtmp != Nullch)
  346.     bestguess = fetchname(indtmp, strippath, TRUE);
  347.     else {
  348.     if (oldtmp != Nullch)
  349.         oldname = fetchname(oldtmp, strippath, TRUE);
  350.     if (newtmp != Nullch)
  351.         newname = fetchname(newtmp, strippath, TRUE);
  352.     if (oldname && newname) {
  353.         if (strlen(oldname) < strlen(newname))
  354.         bestguess = savestr(oldname);
  355.         else
  356.         bestguess = savestr(newname);
  357.     }
  358.     else if (oldname)
  359.         bestguess = savestr(oldname);
  360.     else if (newname)
  361.         bestguess = savestr(newname);
  362.     }
  363.     if (indtmp != Nullch)
  364.     free(indtmp);
  365.     if (oldtmp != Nullch)
  366.     free(oldtmp);
  367.     if (newtmp != Nullch)
  368.     free(newtmp);
  369.     if (indname != Nullch)
  370.     free(indname);
  371.     if (oldname != Nullch)
  372.     free(oldname);
  373.     if (newname != Nullch)
  374.     free(newname);
  375.     return retval;
  376. }
  377.  
  378. /* Remember where this patch ends so we know where to start up again. */
  379.  
  380. void
  381. next_intuit_at(file_pos,file_line)
  382. long file_pos;
  383. long file_line;
  384. {
  385.     p_base = file_pos;
  386.     p_bline = file_line;
  387. }
  388.  
  389. /* Basically a verbose fseek() to the actual diff listing. */
  390.  
  391. void
  392. skip_to(file_pos,file_line)
  393. long file_pos;
  394. long file_line;
  395. {
  396.     char *ret;
  397.  
  398.     assert(p_base <= file_pos);
  399.     if (verbose && p_base < file_pos) {
  400.     Fseek(pfp, p_base, 0);
  401.     say1("The text leading up to this was:\n--------------------------\n");
  402.     while (ftell(pfp) < file_pos) {
  403.         ret = fgets(buf, sizeof buf, pfp);
  404.         assert(ret != Nullch);
  405.         say2("|%s", buf);
  406.     }
  407.     say1("--------------------------\n");
  408.     }
  409.     else
  410.     Fseek(pfp, file_pos, 0);
  411.     p_input_line = file_line - 1;
  412. }
  413.  
  414. /* True if there is more of the current diff listing to process. */
  415.  
  416. bool
  417. another_hunk()
  418. {
  419.     Reg1 char *s;
  420.     Reg8 char *ret;
  421.     Reg2 int context = 0;
  422.  
  423.     while (p_end >= 0) {
  424.     if (p_end == p_efake)
  425.         p_end = p_bfake;        /* don't free twice */
  426.     else
  427.         free(p_line[p_end]);
  428.     p_end--;
  429.     }
  430.     assert(p_end == -1);
  431.     p_efake = -1;
  432.  
  433.     p_max = hunkmax;            /* gets reduced when --- found */
  434.     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
  435.     long line_beginning = ftell(pfp);
  436.                     /* file pos of the current line */
  437.     LINENUM repl_beginning = 0;    /* index of --- line */
  438.     Reg4 LINENUM fillcnt = 0;    /* #lines of missing ptrn or repl */
  439.     Reg5 LINENUM fillsrc;        /* index of first line to copy */
  440.     Reg6 LINENUM filldst;        /* index of first missing line */
  441.     bool ptrn_spaces_eaten = FALSE;    /* ptrn was slightly misformed */
  442.     Reg9 bool repl_could_be_missing = TRUE;
  443.                     /* no + or ! lines in this hunk */
  444.     bool repl_missing = FALSE;    /* we are now backtracking */
  445.     long repl_backtrack_position = 0;
  446.                     /* file pos of first repl line */
  447.     LINENUM repl_patch_line;    /* input line number for same */
  448.     Reg7 LINENUM ptrn_copiable = 0;
  449.                     /* # of copiable lines in ptrn */
  450.  
  451.     ret = pgets(buf, sizeof buf, pfp);
  452.     p_input_line++;
  453.     if (ret == Nullch || strnNE(buf, "********", 8)) {
  454.         next_intuit_at(line_beginning,p_input_line);
  455.         return FALSE;
  456.     }
  457.     p_context = 100;
  458.     p_hunk_beg = p_input_line + 1;
  459.     while (p_end < p_max) {
  460.         line_beginning = ftell(pfp);
  461.         ret = pgets(buf, sizeof buf, pfp);
  462.         p_input_line++;
  463.         if (ret == Nullch) {
  464.         if (p_max - p_end < 4)
  465.             Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  466.         else {
  467.             if (repl_beginning && repl_could_be_missing) {
  468.             repl_missing = TRUE;
  469.             goto hunk_done;
  470.             }
  471.             fatal1("Unexpected end of file in patch.\n");
  472.         }
  473.         }
  474.         p_end++;
  475.         assert(p_end < hunkmax);
  476.         p_char[p_end] = *buf;
  477. #ifdef zilog
  478.         p_line[(short)p_end] = Nullch;
  479. #else
  480.         p_line[p_end] = Nullch;
  481. #endif
  482.         switch (*buf) {
  483.         case '*':
  484.         if (strnEQ(buf, "********", 8)) {
  485.             if (repl_beginning && repl_could_be_missing) {
  486.             repl_missing = TRUE;
  487.             goto hunk_done;
  488.             }
  489.             else
  490.             fatal2("Unexpected end of hunk at line %ld.\n",
  491.                 p_input_line);
  492.         }
  493.         if (p_end != 0) {
  494.             if (repl_beginning && repl_could_be_missing) {
  495.             repl_missing = TRUE;
  496.             goto hunk_done;
  497.             }
  498.             fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
  499.         }
  500.         context = 0;
  501.         p_line[p_end] = savestr(buf);
  502.         if (out_of_mem) {
  503.             p_end--;
  504.             return FALSE;
  505.         }
  506.         for (s=buf; *s && !isdigit(*s); s++) ;
  507.         if (!*s)
  508.             goto malformed;
  509.         if (strnEQ(s,"0,0",3))
  510.             strcpy(s,s+2);
  511.         p_first = (LINENUM) atol(s);
  512.         while (isdigit(*s)) s++;
  513.         if (*s == ',') {
  514.             for (; *s && !isdigit(*s); s++) ;
  515.             if (!*s)
  516.             goto malformed;
  517.             p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  518.         }
  519.         else if (p_first)
  520.             p_ptrn_lines = 1;
  521.         else {
  522.             p_ptrn_lines = 0;
  523.             p_first = 1;
  524.         }
  525.         p_max = p_ptrn_lines + 6;    /* we need this much at least */
  526.         while (p_max >= hunkmax)
  527.             grow_hunkmax();
  528.         p_max = hunkmax;
  529.         break;
  530.         case '-':
  531.         if (buf[1] == '-') {
  532.             if (repl_beginning ||
  533.             (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  534.             {
  535.             if (p_end == 1) {
  536.                 /* `old' lines were omitted - set up to fill */
  537.                 /* them in from 'new' context lines. */
  538.                 p_end = p_ptrn_lines + 1;
  539.                 fillsrc = p_end + 1;
  540.                 filldst = 1;
  541.                 fillcnt = p_ptrn_lines;
  542.             }
  543.             else {
  544.                 if (repl_beginning) {
  545.                 if (repl_could_be_missing){
  546.                     repl_missing = TRUE;
  547.                     goto hunk_done;
  548.                 }
  549.                 fatal3(
  550. "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  551.                     p_input_line, p_hunk_beg + repl_beginning);
  552.                 }
  553.                 else {
  554.                 fatal4(
  555. "%s \"---\" at line %ld--check line numbers at line %ld.\n",
  556.                     (p_end <= p_ptrn_lines
  557.                     ? "Premature"
  558.                     : "Overdue" ),
  559.                     p_input_line, p_hunk_beg);
  560.                 }
  561.             }
  562.             }
  563.             repl_beginning = p_end;
  564.             repl_backtrack_position = ftell(pfp);
  565.             repl_patch_line = p_input_line;
  566.             p_line[p_end] = savestr(buf);
  567.             if (out_of_mem) {
  568.             p_end--;
  569.             return FALSE;
  570.             }
  571.             p_char[p_end] = '=';
  572.             for (s=buf; *s && !isdigit(*s); s++) ;
  573.             if (!*s)
  574.             goto malformed;
  575.             p_newfirst = (LINENUM) atol(s);
  576.             while (isdigit(*s)) s++;
  577.             if (*s == ',') {
  578.             for (; *s && !isdigit(*s); s++) ;
  579.             if (!*s)
  580.                 goto malformed;
  581.             p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
  582.             }
  583.             else if (p_newfirst)
  584.             p_repl_lines = 1;
  585.             else {
  586.             p_repl_lines = 0;
  587.             p_newfirst = 1;
  588.             }
  589.             p_max = p_repl_lines + p_end;
  590.             if (p_max > MAXHUNKSIZE)
  591.             fatal4("Hunk too large (%ld lines) at line %ld: %s",
  592.                   p_max, p_input_line, buf);
  593.             while (p_max >= hunkmax)
  594.             grow_hunkmax();
  595.             if (p_repl_lines != ptrn_copiable)
  596.             repl_could_be_missing = FALSE;
  597.             break;
  598.         }
  599.         goto change_line;
  600.         case '+':  case '!':
  601.         repl_could_be_missing = FALSE;
  602.           change_line:
  603.         if (buf[1] == '\n' && canonicalize)
  604.             strcpy(buf+1," \n");
  605.         if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
  606.           repl_beginning && repl_could_be_missing) {
  607.             repl_missing = TRUE;
  608.             goto hunk_done;
  609.         }
  610.         if (context > 0) {
  611.             if (context < p_context)
  612.             p_context = context;
  613.             context = -1000;
  614.         }
  615.         p_line[p_end] = savestr(buf+2);
  616.         if (out_of_mem) {
  617.             p_end--;
  618.             return FALSE;
  619.         }
  620.         break;
  621.         case '\t': case '\n':    /* assume the 2 spaces got eaten */
  622.         if (repl_beginning && repl_could_be_missing &&
  623.           (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
  624.             repl_missing = TRUE;
  625.             goto hunk_done;
  626.         }
  627.         p_line[p_end] = savestr(buf);
  628.         if (out_of_mem) {
  629.             p_end--;
  630.             return FALSE;
  631.         }
  632.         if (p_end != p_ptrn_lines + 1) {
  633.             ptrn_spaces_eaten |= (repl_beginning != 0);
  634.             context++;
  635.             if (!repl_beginning)
  636.             ptrn_copiable++;
  637.             p_char[p_end] = ' ';
  638.         }
  639.         break;
  640.         case ' ':
  641.         if (!isspace(buf[1]) &&
  642.           repl_beginning && repl_could_be_missing) {
  643.             repl_missing = TRUE;
  644.             goto hunk_done;
  645.         }
  646.         context++;
  647.         if (!repl_beginning)
  648.             ptrn_copiable++;
  649.         p_line[p_end] = savestr(buf+2);
  650.         if (out_of_mem) {
  651.             p_end--;
  652.             return FALSE;
  653.         }
  654.         break;
  655.         default:
  656.         if (repl_beginning && repl_could_be_missing) {
  657.             repl_missing = TRUE;
  658.             goto hunk_done;
  659.         }
  660.         goto malformed;
  661.         }
  662.         /* set up p_len for strncmp() so we don't have to */
  663.         /* assume null termination */
  664.         if (p_line[p_end])
  665.         p_len[p_end] = strlen(p_line[p_end]);
  666.         else
  667.         p_len[p_end] = 0;
  668.     }
  669.     
  670.     hunk_done:
  671.     if (p_end >=0 && !repl_beginning)
  672.         fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
  673.  
  674.     if (repl_missing) {
  675.         
  676.         /* reset state back to just after --- */
  677.         p_input_line = repl_patch_line;
  678.         for (p_end--; p_end > repl_beginning; p_end--)
  679.         free(p_line[p_end]);
  680.         Fseek(pfp, repl_backtrack_position, 0);
  681.         
  682.         /* redundant 'new' context lines were omitted - set */
  683.         /* up to fill them in from the old file context */
  684.         fillsrc = 1;
  685.         filldst = repl_beginning+1;
  686.         fillcnt = p_repl_lines;
  687.         p_end = p_max;
  688.     }
  689.  
  690.     if (diff_type == CONTEXT_DIFF &&
  691.       (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
  692. #ifndef TOS    /* unfortunately this is always true for TOS because of
  693.            the ^M at the end of a line -- just ignore warning */
  694.         if (verbose)
  695.         say4("%s\n%s\n%s\n",
  696. "(Fascinating--this is really a new-style context diff but without",
  697. "the telltale extra asterisks on the *** line that usually indicate",
  698. "the new style...)");
  699. #endif
  700.         diff_type = NEW_CONTEXT_DIFF;
  701.     }
  702.     
  703.     /* if there were omitted context lines, fill them in now */
  704.     if (fillcnt) {
  705.         p_bfake = filldst;        /* remember where not to free() */
  706.         p_efake = filldst + fillcnt - 1;
  707.         while (fillcnt-- > 0) {
  708.         while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  709.             fillsrc++;
  710.         if (fillsrc > p_end)
  711.             fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  712.             p_hunk_beg);
  713.         p_line[filldst] = p_line[fillsrc];
  714.         p_char[filldst] = p_char[fillsrc];
  715.         p_len[filldst] = p_len[fillsrc];
  716.         fillsrc++; filldst++;
  717.         }
  718.         while (fillsrc <= p_end && fillsrc != repl_beginning &&
  719.           p_char[fillsrc] != ' ')
  720.         fillsrc++;
  721. #ifdef DEBUGGING
  722.         if (debug & 64)
  723.         printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  724.             fillsrc,filldst,repl_beginning,p_end+1);
  725. #endif
  726.         assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  727.         assert(filldst==p_end+1 || filldst==repl_beginning);
  728.     }
  729.     }
  730.     else {                /* normal diff--fake it up */
  731.     char hunk_type;
  732.     Reg3 int i;
  733.     LINENUM min, max;
  734.     long line_beginning = ftell(pfp);
  735.  
  736.     p_context = 0;
  737.     ret = pgets(buf, sizeof buf, pfp);
  738.     p_input_line++;
  739.     if (ret == Nullch || !isdigit(*buf)) {
  740.         next_intuit_at(line_beginning,p_input_line);
  741.         return FALSE;
  742.     }
  743.     p_first = (LINENUM)atol(buf);
  744.     for (s=buf; isdigit(*s); s++) ;
  745.     if (*s == ',') {
  746.         p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  747.         while (isdigit(*s)) s++;
  748.     }
  749.     else
  750.         p_ptrn_lines = (*s != 'a');
  751.     hunk_type = *s;
  752.     if (hunk_type == 'a')
  753.         p_first++;            /* do append rather than insert */
  754.     min = (LINENUM)atol(++s);
  755.     for (; isdigit(*s); s++) ;
  756.     if (*s == ',')
  757.         max = (LINENUM)atol(++s);
  758.     else
  759.         max = min;
  760.     if (hunk_type == 'd')
  761.         min++;
  762.     p_end = p_ptrn_lines + 1 + max - min + 1;
  763.     if (p_end > MAXHUNKSIZE)
  764.         fatal4("Hunk too large (%ld lines) at line %ld: %s",
  765.           p_end, p_input_line, buf);
  766.     while (p_end >= hunkmax)
  767.         grow_hunkmax();
  768.     p_newfirst = min;
  769.     p_repl_lines = max - min + 1;
  770.     Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  771.     p_line[0] = savestr(buf);
  772.     if (out_of_mem) {
  773.         p_end = -1;
  774.         return FALSE;
  775.     }
  776.     p_char[0] = '*';
  777.     for (i=1; i<=p_ptrn_lines; i++) {
  778.         ret = pgets(buf, sizeof buf, pfp);
  779.         p_input_line++;
  780.         if (ret == Nullch)
  781.         fatal2("Unexpected end of file in patch at line %ld.\n",
  782.           p_input_line);
  783.         if (*buf != '<')
  784.         fatal2("< expected at line %ld of patch.\n", p_input_line);
  785.         p_line[i] = savestr(buf+2);
  786.         if (out_of_mem) {
  787.         p_end = i-1;
  788.         return FALSE;
  789.         }
  790.         p_len[i] = strlen(p_line[i]);
  791.         p_char[i] = '-';
  792.     }
  793.     if (hunk_type == 'c') {
  794.         ret = pgets(buf, sizeof buf, pfp);
  795.         p_input_line++;
  796.         if (ret == Nullch)
  797.         fatal2("Unexpected end of file in patch at line %ld.\n",
  798.             p_input_line);
  799.         if (*buf != '-')
  800.         fatal2("--- expected at line %ld of patch.\n", p_input_line);
  801.     }
  802.     Sprintf(buf, "--- %ld,%ld\n", min, max);
  803.     p_line[i] = savestr(buf);
  804.     if (out_of_mem) {
  805.         p_end = i-1;
  806.         return FALSE;
  807.     }
  808.     p_char[i] = '=';
  809.     for (i++; i<=p_end; i++) {
  810.         ret = pgets(buf, sizeof buf, pfp);
  811.         p_input_line++;
  812.         if (ret == Nullch)
  813.         fatal2("Unexpected end of file in patch at line %ld.\n",
  814.             p_input_line);
  815.         if (*buf != '>')
  816.         fatal2("> expected at line %ld of patch.\n", p_input_line);
  817.         p_line[i] = savestr(buf+2);
  818.         if (out_of_mem) {
  819.         p_end = i-1;
  820.         return FALSE;
  821.         }
  822.         p_len[i] = strlen(p_line[i]);
  823.         p_char[i] = '+';
  824.     }
  825.     }
  826.     if (reverse)            /* backwards patch? */
  827.     if (!pch_swap())
  828.         say1("Not enough memory to swap next hunk!\n");
  829. #ifdef DEBUGGING
  830.     if (debug & 2) {
  831.     int i;
  832.     char special;
  833.  
  834.     for (i=0; i <= p_end; i++) {
  835.         if (i == p_ptrn_lines)
  836.         special = '^';
  837.         else
  838.         special = ' ';
  839.         fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
  840.         Fflush(stderr);
  841.     }
  842.     }
  843. #endif
  844.     if (p_end+1 < hunkmax)    /* paranoia reigns supreme... */
  845.     p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
  846.     return TRUE;
  847.  
  848. malformed:
  849.     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
  850.         /* about as informative as "Syntax error" in C */
  851.     return FALSE;    /* for lint */
  852. }
  853.  
  854. /* Input a line from the patch file, worrying about indentation. */
  855.  
  856. char *
  857. pgets(bf,sz,fp)
  858. char *bf;
  859. int sz;
  860. FILE *fp;
  861. {
  862.     char *ret = fgets(bf, sz, fp);
  863.     Reg1 char *s;
  864.     Reg2 int indent = 0;
  865.  
  866.     if (p_indent && ret != Nullch) {
  867.     for (s=buf;
  868.       indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
  869.         if (*s == '\t')
  870.         indent += 8 - (indent % 7);
  871.         else
  872.         indent++;
  873.     }
  874.     if (buf != s)
  875.         Strcpy(buf, s);
  876.     }
  877.     return ret;
  878. }
  879.  
  880. /* Reverse the old and new portions of the current hunk. */
  881.  
  882. bool
  883. pch_swap()
  884. {
  885.     char **tp_line;        /* the text of the hunk */
  886.     short *tp_len;        /* length of each line */
  887.     char *tp_char;        /* +, -, and ! */
  888.     Reg1 LINENUM i;
  889.     Reg2 LINENUM n;
  890.     bool blankline = FALSE;
  891.     Reg3 char *s;
  892.  
  893.     i = p_first;
  894.     p_first = p_newfirst;
  895.     p_newfirst = i;
  896.     
  897.     /* make a scratch copy */
  898.  
  899.     tp_line = p_line;
  900.     tp_len = p_len;
  901.     tp_char = p_char;
  902.     p_line = Null(char**);    /* force set_hunkmax to allocate again */
  903.     p_len = Null(short*);
  904.     p_char = Nullch;
  905.     set_hunkmax();
  906.     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
  907. #ifndef lint
  908.     if (p_line == Null(char**))
  909.         free((char*)p_line);
  910.     p_line = tp_line;
  911.     if (p_len == Null(short*))
  912.         free((char*)p_len);
  913.     p_len = tp_len;
  914. #endif
  915.     if (p_char == Nullch)
  916.         free((char*)p_char);
  917.     p_char = tp_char;
  918.     return FALSE;        /* not enough memory to swap hunk! */
  919.     }
  920.  
  921.     /* now turn the new into the old */
  922.  
  923.     i = p_ptrn_lines + 1;
  924.     if (tp_char[i] == '\n') {        /* account for possible blank line */
  925.     blankline = TRUE;
  926.     i++;
  927.     }
  928.     if (p_efake >= 0) {            /* fix non-freeable ptr range */
  929.     if (p_efake <= i)
  930.         n = p_end - i + 1;
  931.     else
  932.         n = -i;
  933.     p_efake += n;
  934.     p_bfake += n;
  935.     }
  936.     for (n=0; i <= p_end; i++,n++) {
  937.     p_line[n] = tp_line[i];
  938.     p_char[n] = tp_char[i];
  939.     if (p_char[n] == '+')
  940.         p_char[n] = '-';
  941.     p_len[n] = tp_len[i];
  942.     }
  943.     if (blankline) {
  944.     i = p_ptrn_lines + 1;
  945.     p_line[n] = tp_line[i];
  946.     p_char[n] = tp_char[i];
  947.     p_len[n] = tp_len[i];
  948.     n++;
  949.     }
  950.     assert(p_char[0] == '=');
  951.     p_char[0] = '*';
  952.     for (s=p_line[0]; *s; s++)
  953.     if (*s == '-')
  954.         *s = '*';
  955.  
  956.     /* now turn the old into the new */
  957.  
  958.     assert(tp_char[0] == '*');
  959.     tp_char[0] = '=';
  960.     for (s=tp_line[0]; *s; s++)
  961.     if (*s == '*')
  962.         *s = '-';
  963.     for (i=0; n <= p_end; i++,n++) {
  964.     p_line[n] = tp_line[i];
  965.     p_char[n] = tp_char[i];
  966.     if (p_char[n] == '-')
  967.         p_char[n] = '+';
  968.     p_len[n] = tp_len[i];
  969.     }
  970.     assert(i == p_ptrn_lines + 1);
  971.     i = p_ptrn_lines;
  972.     p_ptrn_lines = p_repl_lines;
  973.     p_repl_lines = i;
  974. #ifndef lint
  975.     if (tp_line == Null(char**))
  976.     free((char*)tp_line);
  977.     if (tp_len == Null(short*))
  978.     free((char*)tp_len);
  979. #endif
  980.     if (tp_char == Nullch)
  981.     free((char*)tp_char);
  982.     return TRUE;
  983. }
  984.  
  985. /* Return the specified line position in the old file of the old context. */
  986.  
  987. LINENUM
  988. pch_first()
  989. {
  990.     return p_first;
  991. }
  992.  
  993. /* Return the number of lines of old context. */
  994.  
  995. LINENUM
  996. pch_ptrn_lines()
  997. {
  998.     return p_ptrn_lines;
  999. }
  1000.  
  1001. /* Return the probable line position in the new file of the first line. */
  1002.  
  1003. LINENUM
  1004. pch_newfirst()
  1005. {
  1006.     return p_newfirst;
  1007. }
  1008.  
  1009. /* Return the number of lines in the replacement text including context. */
  1010.  
  1011. LINENUM
  1012. pch_repl_lines()
  1013. {
  1014.     return p_repl_lines;
  1015. }
  1016.  
  1017. /* Return the number of lines in the whole hunk. */
  1018.  
  1019. LINENUM
  1020. pch_end()
  1021. {
  1022.     return p_end;
  1023. }
  1024.  
  1025. /* Return the number of context lines before the first changed line. */
  1026.  
  1027. LINENUM
  1028. pch_context()
  1029. {
  1030.     return p_context;
  1031. }
  1032.  
  1033. /* Return the length of a particular patch line. */
  1034.  
  1035. short
  1036. pch_line_len(line)
  1037. LINENUM line;
  1038. {
  1039.     return p_len[line];
  1040. }
  1041.  
  1042. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1043.  
  1044. char
  1045. pch_char(line)
  1046. LINENUM line;
  1047. {
  1048.     return p_char[line];
  1049. }
  1050.  
  1051. /* Return a pointer to a particular patch line. */
  1052.  
  1053. char *
  1054. pfetch(line)
  1055. LINENUM line;
  1056. {
  1057.     return p_line[line];
  1058. }
  1059.  
  1060. /* Return where in the patch file this hunk began, for error messages. */
  1061.  
  1062. LINENUM
  1063. pch_hunk_beg()
  1064. {
  1065.     return p_hunk_beg;
  1066. }
  1067.  
  1068. #ifndef TOS
  1069. /* Apply an ed script by feeding ed itself. */
  1070.  
  1071. void
  1072. do_ed_script()
  1073. {
  1074.     Reg1 char *t;
  1075.     Reg2 long beginning_of_this_line;
  1076.     Reg3 bool this_line_is_command = FALSE;
  1077.     Reg4 FILE *pipefp;
  1078.     FILE *popen();
  1079.  
  1080.     if (!skip_rest_of_patch) {
  1081.     Unlink(TMPOUTNAME);
  1082.     copy_file(filearg[0], TMPOUTNAME);
  1083.     if (verbose)
  1084.         Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  1085.     else
  1086.         Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  1087.     pipefp = popen(buf, "w");
  1088.     }
  1089.     for (;;) {
  1090.     beginning_of_this_line = ftell(pfp);
  1091.     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  1092.         next_intuit_at(beginning_of_this_line,p_input_line);
  1093.         break;
  1094.     }
  1095.     p_input_line++;
  1096.     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  1097.     this_line_is_command = (isdigit(*buf) &&
  1098.       (*t == 'd' || *t == 'c' || *t == 'a') );
  1099.     if (this_line_is_command) {
  1100.         if (!skip_rest_of_patch)
  1101.         fputs(buf, pipefp);
  1102.         if (*t != 'd') {
  1103.         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  1104.             p_input_line++;
  1105.             if (!skip_rest_of_patch)
  1106.             fputs(buf, pipefp);
  1107.             if (strEQ(buf, ".\n"))
  1108.             break;
  1109.         }
  1110.         }
  1111.     }
  1112.     else {
  1113.         next_intuit_at(beginning_of_this_line,p_input_line);
  1114.         break;
  1115.     }
  1116.     }
  1117.     if (skip_rest_of_patch)
  1118.     return;
  1119.     fprintf(pipefp, "w\n");
  1120.     fprintf(pipefp, "q\n");
  1121.     Fflush(pipefp);
  1122.     Pclose(pipefp);
  1123.     ignore_signals();
  1124.     if (move_file(TMPOUTNAME, outname) < 0) {
  1125.     toutkeep = TRUE;
  1126.     chmod(TMPOUTNAME, filemode);
  1127.     }
  1128.     else
  1129.     chmod(outname, filemode);
  1130.     set_signals(1);
  1131. }
  1132. #else
  1133. void
  1134. do_ed_script()
  1135. {
  1136.     fprintf(stderr,"Sorry! TOS version of Patch cannot deal with ed scripts\n");
  1137.     exit(999);
  1138. }
  1139. #endif
  1140.